package com.lkm.market2_0.m.steelyardserver.to;

import com.lkm.javatool.BitHelp;
import com.lkm.javatool.NumberHelp;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;




public class TradeInfoEntity  {

    private int no;

    private int num;

    private String name;

    private float weight;

    private float unitPrice;

    private float totalPrice;

    private String date;


    

    public static TradeInfoEntity createByData(byte [] data){
        if(data.length<32){
            return null;
        }

        TradeInfoEntity tradeInfoEntity=null;
        try {
            tradeInfoEntity = new TradeInfoEntity();

            tradeInfoEntity.setNo(NumberHelp.bytesToIntDesc(new byte[]{data[0], data[1]}));
            try {
                tradeInfoEntity.setName(new String(Arrays.copyOfRange(data, 2, 14), "gbk"));
                tradeInfoEntity.setName(tradeInfoEntity.getName().replaceAll("；", ""));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }



            
            boolean isWeightF= BitHelp.hasFlagBit(data[14],128);
            
            boolean isNum=BitHelp.hasFlagBit(data[14],64);

            
            data[14]=(byte) BitHelp.removeFlagBit(data[14],128);
            data[14]=(byte) BitHelp.removeFlagBit(data[14],68);

            if(isNum){

                tradeInfoEntity.setNum(NumberHelp.bytesToIntDesc(new byte[]{data[14], data[15], data[16], data[17]}));
                if(isWeightF){
                    tradeInfoEntity.setNum(tradeInfoEntity.getNum()*-1);
                }
            }else{
                tradeInfoEntity.setWeight(NumberHelp.bytesToIntDesc(new byte[]{data[14], data[15], data[16], data[17]}) / 1000.0f);
                if(isWeightF){
                    tradeInfoEntity.setWeight(tradeInfoEntity.getWeight()*-1);
                }
            }



            tradeInfoEntity.setUnitPrice(NumberHelp.bytesToIntDesc(new byte[]{data[18], data[19], data[20], data[21]}) / 100.0f);

            tradeInfoEntity.setTotalPrice(NumberHelp.bytesToIntDesc(new byte[]{data[22], data[23], data[24], data[25]}) / 100.0f);


            
            tradeInfoEntity.setDate("20" + NumberHelp.bytesToHex(data[26]) + "-" + NumberHelp.bytesToHex(data[27]) + "-" + NumberHelp.bytesToHex(data[28]) +
                    " " + NumberHelp.bytesToHex(data[29]) +
                    ":" + NumberHelp.bytesToHex(data[30]) +
                    ":" + NumberHelp.bytesToHex(data[31])
            );
        }catch (Exception e){
            e.printStackTrace();
        }

        return tradeInfoEntity;
    }


    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public float getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(float unitPrice) {
        this.unitPrice = unitPrice;
    }

    public float getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(float toalPrice) {
        this.totalPrice = toalPrice;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
